<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>HappyERP</title>

    <%= stylesheet_link_tag "/javascripts/ext/resources/css/ext-all.css" %>
    <%= stylesheet_link_tag "images" %>
    <%= stylesheet_link_tag "ext-patch" %>
    <%= stylesheet_link_tag "/javascripts/ext/ux/css/ColumnNodeUI.css" %>
    <%= stylesheet_link_tag "/javascripts/ext/ux/treegrid/treegrid.css" %>
    <%= javascript_include_tag "LodopFuncs.js" %>
    <object  id="LODOP" classid="clsid:2105C259-1E0C-4534-8141-A753534CB4CA" width=0 height=0>
      <embed id="LODOP_EM" type="application/x-print-lodop" width=0 height=0></embed>
    </object>
    <%= javascript_include_tag "ext/adapter/ext/ext-base.js" %>
    <%= javascript_include_tag "ext/ext-all.js" %>


    <%= javascript_include_tag "ext/src/locale/ext-lang-zh_CN.js" %>
    <%= javascript_include_tag "ext/ux/ColumnNodeUI.js" %>
    <%= javascript_include_tag "ext/ux/TabCloseMenu.js" %>

    <%= csrf_meta_tag %>

    <script type="text/javascript">
      //print
      var LODOP=getLodop(document.getElementById('LODOP'),document.getElementById('LODOP_EM'));
      
      var authToken = new Ext.form.Hidden({
        name: 'authenticity_token',
        value: '<%=Rack::Utils.escape_html(form_authenticity_token)%>'
      });

      //rejectKeyArray是个比较通用的排除数组，一般的提交都不需要提交这些信息
      var rejectKeyArray = new Array();
      rejectKeyArray.push('product_name');
      rejectKeyArray.push('is_suit');
      rejectKeyArray.push('number');
      rejectKeyArray.push('spec');
      var financialManagement = "<%=session[:financial_management]%>";
      Ext.QuickTips.init();
      Ext.lib.Ajax.defaultPostHeader += ";charset=UTF-8";
      Ext.data.Connection.prototype.method = 'GET';
      //这是一个小补丁，阻止在IE下按Backspce引起乱跳转页面
      new Ext.KeyMap(document, [{
          key: Ext.EventObject.BACKSPACE,
          fn: function (key, e) {
            var t = e.target.tagName;
            if (t !== "INPUT" && t !== "TEXTAREA") {
              e.stopEvent();
            }
          }
        }]);

      //把内部的把backspace退回一页的的问题，屏蔽掉。
      Ext.getDoc().on('keydown',function(e){
        if(e.getKey() == 8 && e.getTarget().type =='text' && !e.getTarget().readOnly){

        }else if(e.getKey() == 8 && e.getTarget().type =='textarea' && !e.getTarget().readOnly){

        }else if(e.getKey() == 8){
          e.preventDefault();
        }
      });


      Ext.apply(Ext.form.VTypes, {
        daterange : function(val, field) {
          var date = field.parseDate(val);

          if(!date){
            return false;
          }
          if (field.startDateField) {
            var start = Ext.getCmp(field.startDateField);
            if (!start.maxValue || (date.getTime() != start.maxValue.getTime())) {
              start.setMaxValue(date);
              start.validate();
            }
          }
          else if (field.endDateField) {
            var end = Ext.getCmp(field.endDateField);
            if (!end.minValue || (date.getTime() != end.minValue.getTime())) {
              end.setMinValue(date);
              end.validate();
            }
          }
          /*
           * Always return true since we're only using this vtype to set the
           * min/max allowed values (these are tested for after the vtype test)
           */
          return true;
        },

        password : function(val, field) {
          if (field.initialPassField) {
            var pwd = Ext.getCmp(field.initialPassField);
            return (val == pwd.getValue());
          }
          return true;
        },

        passwordText : '二次输入的密码不匹配'
      });

      function logout(){
        Ext.Ajax.request({
          url : '/index/logout',
          success : function (result, request) {
            window.document.location.href = '/index/login';
          },
          failure : function (result, request){
            Ext.MessageBox.show({
              title: '消息',
              msg: "通讯失败，请从新操作",
              buttons: Ext.MessageBox.OK,
              icon: Ext.MessageBox.WARNING
            });
          },
          method : 'GET'
        });
      }

      var synUnreadSysMsg = null;
        
      Ext.onReady(function() {
        var sysMsgButton = new Ext.Button({
          scope:this,
          text: '系统消息',
          iconCls: 'msg',
          handler: function(){
            if (workTabs.getItem(1) == undefined){
              
              workTabs.add({
                title: '系统消息',
                autoLoad: {
                  url: '/sys_msg/sysMsgList',
                  scripts: true
                },
                closable:true
              }).show();
               
            }else{
              workTabs.getItem(1).load({
                url: '/sys_msg/sysMsgList',
                scripts: true
              });
              //workTabs.getItem(0).getUpdater().update(url);
              workTabs.getItem(1).show();
            }
          }
        })

        var logoutButton = new Ext.Button({
          text: '退出系统',
          iconCls: 'exit',
          handler: function(){
            Ext.Ajax.request({
              url : '/index/logout',
              success : function (result, request) {
                window.document.location.href = '/index/login';
              },
              failure : function (result, request){
                Ext.MessageBox.show({
                  title: '消息',
                  msg: "通讯失败，请从新操作",
                  buttons: Ext.MessageBox.OK,
                  icon: Ext.MessageBox.WARNING
                });
              },
              method : 'GET'
            });
          }
        })
        
        var topPanel = new Ext.Panel({
          frame:false,
          bbar: ['<b style="font-size:14px;"><%=@companyNameAB%>， 您好：<%=session[:employee_name]%>', '->','-',sysMsgButton,'-',logoutButton],
          cls:'empty',
          region:'north'
        });       

        //左边的菜单栏
        var accordition = new Ext.Panel({
          region:'west',
          margins:'5 0 5 5',
          split:true,
          width: 140,
          collapseMode: 'mini',
          layout:'accordion'
        });
        
        //工作区
        var workTabs = new Ext.TabPanel({
          //renderTo:'tabs',
          region:'center',
          activeTab: 0,
          resizeTabs:true, // turn on tab resizing
          minTabWidth: 115,
          tabWidth:135,
          enableTabScroll:true,
          //width:600,
          //height:250,
          defaults: {autoScroll:true},
          plugins: new Ext.ux.TabCloseMenu(),
          initEvents : function(){
            Ext.TabPanel.superclass.initEvents.call(this);
            this.on('add', this.onAdd, this, {target: this});
            this.on('remove', this.onRemove, this, {target: this});
            this.mon(this.strip, 'mousedown', this.onStripMouseDown, this);
            this.mon(this.strip, 'contextmenu', this.onStripContextMenu, this);
            if(this.enableTabScroll){
              this.mon(this.strip, 'mousewheel', this.onWheel, this);
            }
            //ADD:monitor title dbclick
            this.mon(this.strip,'dblclick',this.onTitleDbClick,this);
          },
          //ADD: handler
          onTitleDbClick:function(e,target,o){
            accordition.toggleCollapse(true);
          }
        });

        var reminderPanel = new Ext.Panel({
          title: '系统提醒',
          id:'reminder_panel',
          //html: '&lt;empty panel&gt;',
          //cls:'cellpadding:10',
          defaults:{margins:'5 5 5 5'},
          //bodyStyle: 'padding:15px;align:center',
          listeners: {
            //expand: showAgileDiagram
          }
        });
        workTabs.add(reminderPanel).show();

        workTabs.openTab = function(node) {
          // var id = node.attributes.url;
          var id = node.attributes.url;
          var url = node.attributes.url;
          var tabTitle = node.text;
          var titleStyle = ['background:url(\'', node.ui.getIconEl().src, '\') no-repeat; padding-left:20px'].join('');
          
          workTabs.getItem(0).setTitle(tabTitle);
          workTabs.getItem(0).load({
            url: url,
            scripts: true
          });
          //workTabs.getItem(0).getUpdater().update(url);
          workTabs.getItem(0).show();
        };
        
        var viewport = new Ext.Viewport({
          layout:'border',
          items:[topPanel,accordition, workTabs]
        });

        //添加详细菜单
        function renderMenu(){
          var menus = eval(<%=raw @menus%>);
          if (!menus) { return; }
          for (var i = 0; i < menus.length; i++) {
            var item = menus[i];

            var title = "<div style='background:url(" + item.image +
              ") no-repeat;padding-left:20px;'>" +
              item.name +
              "</div>";
            
            var node = new Ext.tree.TreePanel({
              title: title,
              rootVisible: false,
              lines: false,
              autoScroll: true,
              qtips: item.qitps,
              root: {
                editable: false,
                expanded: true,
                text: item.name,
                draggable: false,
                children: item.children
              },
              listeners: {
                click: function(node, e) {
                  workTabs.openTab(node);
                }
              }
            });
            accordition.add(node);
          }
          accordition.doLayout();
        }
        renderMenu();

        synUnreadSysMsg = function(){
          Ext.Ajax.request({
            url : '/sys_msg/getUnreadSysMsgsSize',
            success : function (result, request) {
              if (parseInt(result.responseText) > 0){
                sysMsgButton.setText('系统消息<font color=red>('+parseInt(result.responseText)+')</font>');
              }else{
                sysMsgButton.setText('系统消息');
              }
            },
            failure : function (result, request){
            },
            method : 'GET'
          });
        }
        
        //定时查看系统消息
        Ext.TaskMgr.start({
          run: synUnreadSysMsg,
          interval: 300000
        })
      
      });      
    </script>
  </head>
  <body>
    <%= javascript_include_tag "/javascripts/application.js" %>
  </body>
</html>
